
//获取操作人的姓名及ID
var loginData=JSON.parse(web.readCookie("loginData"));
var service_name = loginData.user.username;
var service_id = loginData.user.user_ID;
// 重置功能
$('#resetbtn').click(function () {
    document.getElementById("form-search").reset();
    // $('#inquiry_status').val('');
    //$("#submitBtn").trigger('click');
})
var dataSource = new Array();
//table的数据源
var table; //table对象
dataSource = [];
// 通过查询获得数据
function getList(){
    $.ajax({
        contentType: 'application/json',
        // url: root.orderInter.order_cancel_apply,
        url: root.dockingplatform.queryCancelApplyByPlatForm,
        cache: false, //禁用缓存
        type: "POST",
        data: JSON.stringify({
            page_size: 20,
            page_num: 1,
            order_id: $("#order_id").val(),
            platform: $("#platform").val(),
            customer_name: $("#customer_name").val(),
            deal_status: $("#deal_status").val(),
            created_at_begin: $("#timemin").val(),
            created_at_end: $("#timemax").val(),
            org_warehouse_id:sessionStorage.getItem("organizationWarehouse"),//公司对应的发货仓id
            warehouse_ids:sessionStorage.getItem("regions"),//仓库权限串
            belong_groupIds:sessionStorage.getItem("companys"),//销售归属权限串
            sales_manager_ids:sessionStorage.getItem("personnels"),//销售经理权限串
            sent_type:$("#sent_type").val(),//配送类型
            apply_role:$("#apply_role").val()

        }),
        success: function(data) {

            sessionStorage.setItem("dealstatus1",'')
            sessionStorage.setItem("dealstatus2",'')
            goodsList(data);
            totalPage = data.data.totalPage;
            $('.M-box3').pagination({
                pageCount: totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function(api) {
                    console.log(api.getCurrent());
                    $.ajax({
                        contentType: 'application/json',
                        // url: root.orderInter.order_cancel_apply,
                        url: root.dockingplatform.queryCancelApplyByPlatForm,
                        cache: false, //禁用缓存
                        type: "POST",
                        data: JSON.stringify({
                            page_size: 20,
                            page_num: api.getCurrent(),
                            order_id: $("#order_id").val(),
                            platform: $("#platform").val(),
                            customer_name: $("#customer_name").val(),
                            deal_status: $("#deal_status").val(),
                            // deal_status: $("#deal_status").val(),
                            created_at_begin: $("#timemin").val(),
                            created_at_end: $("#timemax").val(),
                            org_warehouse_id:sessionStorage.getItem("organizationWarehouse"),//公司对应的发货仓id
                            warehouse_ids:sessionStorage.getItem("regions"),//仓库权限串
                            belong_groupIds:sessionStorage.getItem("companys"),//销售归属权限串
                            sales_manager_ids:sessionStorage.getItem("personnels"),//销售经理权限串
                            sent_type:$("#sent_type").val(),//配送类型
                            apply_role:$("#apply_role").val()
                        }),
                        success: function(data) {
                            goodsList(data);
                        }
                    });
                }
            });
        }
    });
}

function goodsList(data) {
    sessionStorage.setItem("authorizedUsers", JSON.stringify(data));
    dataSource.splice(0,dataSource.length);//清空数组

    if(data.data.result!=null){
        var datashu =data.data.startIndex+1;
        $.each(data.data.result, function(i, field) {
            var datashus = datashu++
            var rowData = new Array();
            rowData.push(field.order_id);
            rowData.push(field.platform);
            rowData.push(field.customer_name);
            rowData.push(field.created_at);
            rowData.push(field.order_created_at);
            /*rowData.push(field.final_paid_type);*/
            rowData.push(field.paid_type);
            rowData.push(field.paid_amount);
            rowData.push(field.deal_status);
            rowData.push(field.id);
            rowData.push(field.sent_type);
            rowData.push(datashus);
            dataSource.push(rowData);
        });
    }

    initDataTable(dataSource);

    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "autoWidth": false,
            "ordering": false,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [50, 25, 10, 5],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[10]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },


                {
                    "render": function(data, type, row, meta) {
                        state=row[1];
                        var platform='';
                        if(state == '10'){
                            platform= '普通平台';
                            //operation = '匹配';
                        }else if(state == '20'){
                            platform= '签约平台';
                            //operation = '匹配';
                        }else if(state == '40'){
                            platform= '对接平台';
                            //operation = '匹配';
                        }
                        var val= '<span platform_value="'+state+'">'+platform+'</span>';
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 5
                },
               /* {
                    "render": function(data, type, row, meta) {
                        state=row[5];
                        var final_paid_type='';
                        if (state == null){
                            var value= '<span >'+final_paid_type+'</span>';
                            return value;
                        }
                        var obj1 = eval(state);
                        var payCode = obj1[0].code; 

                        if(payCode == '1002'){
                            final_paid_type= '支付宝支付';
                        }else if(payCode == '1003'){
                            final_paid_type= '微信支付';
                        }else if(payCode == '1101'){
                            final_paid_type= '货到付款';
                        }else if(payCode == '1102'){
                            final_paid_type= '货到付款-现金';
                        }else if(payCode == '1103'){
                            final_paid_type= '货到付款-pos机';
                        }else if(payCode == '1201'){
                            final_paid_type= '账期支付';
                        }else if(payCode == '1301'){
                            final_paid_type= '银行转账';
                        }else if(payCode == '1401'){
                            final_paid_type= '电子汇票';
                        }else if(payCode == '1501'){
                            final_paid_type= '预付款';
                        }
                        var val= '<span final_paid_value="'+payCode+'">'+final_paid_type+'</span>';
                        return val;
                    },
                    "targets": 5
                },*/
                {
                    "render": function(data, type, row, meta) {
                        state=row[5];
                        var final_paid_type='';
                        if (state == null){
                            var value= '<span >'+final_paid_type+'</span>';
                            return value;
                        }

                        if(state == '1001'){
                            final_paid_type= '在线支付';
                        }else if(state == '1101'){
                            final_paid_type= '货到付款';
                        }else if(state == '1102'){
                            final_paid_type= '货到付款-现金';
                        }else if(state == '1103'){
                            final_paid_type= '货到付款-pos机';
                        }else if(state == '1201'){
                            final_paid_type= '账期支付';
                        }else if(state == '1301'){
                            final_paid_type= '银行转账';
                        }else if(state == '1401'){
                            final_paid_type= '银行汇票';
                        }
                        var val= '<span deal_value="'+state+'">'+final_paid_type+'</span>';
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {
                        var senttype = "";
                        switch (row[9]) {
                            case 10:
                                senttype = "快递";
                                break;
                            case 20:
                                senttype = "快运";
                                break;
                            case 21:
                                senttype = "快运";
                                break;
                            case 30:
                                senttype = "专车包车";
                                break;
                            case 40:
                                senttype = "鑫方盛物流";
                                break;
                            case 50:
                                senttype = "上门自提";
                                break;

                        }
                        var val = '<span>' + senttype + '</span>';
                        return val;
                    },
                    "targets": 7
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = ""
                        if (row[6] == undefined) {
                            val = ''
                        }else {
                            val =row[6].toFixed(2)
                        }
                        return val;
                    },
                    "targets": 8
                },
                {
                    "render": function(data, type, row, meta) {
                        state=row[7];
                        var deal_status='';
                        if(state == '10'){
                            deal_status= '待处理';
                        }else if(state == '20'){
                            deal_status= '处理中';
                        }else if(state == '30'){
                            deal_status= '已处理';
                        }else if(state == '40'){
                            deal_status= '已关闭';
                        }
                        var val= '<span deal_value="'+state+'">'+deal_status+'</span>';
                        return val;
                    },
                    "targets": 9
                },
                {
                    "render": function(data, type, row, meta) {
                        var val = '<span class="powerButton 619"><a onclick="orderdetail(\'取消订单申请详情\',\'cancelOrderDetails.html\',\''+row[0]+'\',\''+row[8]+'\',1)">查看详情</a></span>';

                        return val;
                    },
                    "targets": 10
                }
            ]
        });
        //权限控制函数
        power()
    };
}
/*跳转订单详情*/
function orderdetail(title,url,data,pid,type){
    sessionStorage.setItem("orderIId",data);
    sessionStorage.setItem("pid",pid);
    sessionStorage.setItem("type",type);

    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
//弹框跳转
var dealstatus1= sessionStorage.getItem("dealstatus1")//待处理
var dealstatus2= sessionStorage.getItem("dealstatus2")//处理中
if(dealstatus1==10){
    $('#deal_status').val(dealstatus1)
    getList()
    // sessionStorage.setItem("dealstatus1",'')

}
if(dealstatus2==20){
    $('#deal_status').val(dealstatus2)
    getList()
    // sessionStorage.setItem("dealstatus2",'')
}